package traverser;

import java.net.URL;
import java.net.HttpURLConnection;

/*
 * frequencycollector
 * pick startPage and EndPage randomly
 * construct a TT on it;
 * TT.traverse();
 * foreach rage in TT.getPath();
 * fq.increment(page);
 */

public class FrequencyCollector {
	static String sp = null;
	static String ep = null;
	static int[] b = new int[10000];
	static int count = 0;

	public static void main(String[] args) {
		int iterations = 1;
		if (args.length >= 1) {
			iterations = Integer.parseInt(args[0]);
		}
		
		boolean quiet = iterations != 1; 
		
		for (int i = 0; i < iterations; i++) {
			
			FrequencyCounter fq = new FrequencyCounter("test.freq");
			
			sp=getRandomPage(); 
			ep=getRandomPage();
			
			System.out.println(sp);
			System.out.println(ep);
			
			KeywordTraverser myTraverser = new KeywordTraverser(sp, ep);
			int count = myTraverser.traverse(quiet);
			System.out.println("Iteration " + i + " count " + count);
			myTraverser.printPath();
	
			for (String page : myTraverser.getPath().trim()) {
				fq.increment(page);
			}
			fq.write();
		}
	}

	public static String getRandomPage() {
		URL site = null;
		HttpURLConnection conn = null;
		try {
			site = new URL("http://en.wikipedia.org/wiki/Special:Random");
			conn = (HttpURLConnection) site.openConnection();
			conn.setConnectTimeout(15000);
			conn.setReadTimeout(15000);
			conn.setRequestProperty("User-Agent", "Telnet");
			conn.connect();
			conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
		return conn.getURL().toString();
	}
}
